#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#define BUFLEN 8
#define PORT 36666
#define SRV_IP "10.255.255.249"


void diep(char *s)
{
	printf(s);
	exit(1);
}




int host_to_ip(char * hostname , char* ip)
{
    struct hostent *he;
    struct in_addr **addr_list;
    int i;

    if ( (he = gethostbyname( hostname ) ) == NULL)
    {
        // get the host info
        diep("gethostbyname");
        return 1;
    }
    addr_list = (struct in_addr **) he->h_addr_list;
    for(i = 0; addr_list[i] != NULL; i++)
    {
        //Return the first one;
        strcpy(ip , inet_ntoa(*addr_list[i]) );
        return 0;
    }
    return 1;
}

int main (int argc, char *argv[])
{

	struct sockaddr_in si_other;
	struct in_addr localInterface;
	char buf[BUFLEN] = "\0";
	int ret, len;
	char  ip[BUFLEN], hostname[BUFLEN];
	char loopch = 1;
	struct sockaddr_in group_sock;
	int s;
		
	if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    diep("socket");
	
	memset( &group_sock, 0, sizeof(group_sock));
	group_sock.sin_family = AF_INET;
	group_sock.sin_addr.s_addr = inet_addr("226.1.1.1");
	group_sock.sin_port = htons(36666);
	
	//gethostname(hostname, BUFLEN);
	/*
	if( -1 == (ret = host_to_ip(hostname, ip)) )
	{
		diep("hostname_to_ip");
		return -1;
	}
	*/
	localInterface.s_addr = inet_addr("10.1.1.1");
	
	if(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0)
	{
		perror("Setting local interface error");
		exit(1);
	}	
	else
	{
		printf("Setting the local interface...OK\n");
	}
	
	strcpy(buf, "ALL");
	len = sizeof( struct sockaddr);
		
	if (sendto(s, buf, BUFLEN, 0, (struct sockaddr*)&group_sock, sizeof(group_sock) )==-1)
	{
		diep("sendto()");
	}

	close(s);
	return 0;
}



